global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/docdb_families_ipc_codes.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {


* docbd_families_ipc_codes.do
* This do-file aggregates the patent-technology dataset to patent families.*

use appln_id docdb_family_id using ${commondata_dir}/patstat_2018b/family_info.dta, clear
mmerge appln_id using ${dataset_dir}/patstat_orbis/cipc_codes.dta, unmatched(none)
keep docdb_family_id cipc6
duplicates drop

sort docdb cipc6
save ${dataset_dir}/patstat_orbis/docdb_family_id_cipc_codes.dta, replace
export delimited using ${dataset_dir}/patstat_orbis/docdb_family_id_cipc_codes.csv, replace 

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat